#!/bin/sh

# Website: http://www.it-adv.net/
# Author:  KelvinHan
# QQ:191848423
# MSN/EMAIL:shichangguo@msn.com
# 2009-2-28

ROOT=/fx
CMD_PATH=/var/www/commands
cd $ROOT

P_NAME="fetion"

for line in `cat robots.dat`
do
	f_account=`echo $line|awk -F_ '{print $1}'`
	f_pwd=`echo $line|awk -F_ '{print $2}'`

	echo "Check fetion account $f_account....."
	PID=`ps ax|grep $P_NAME|grep $f_account|awk '{print $1}'`
	echo "PID of fetion:$f_account is $PID!"
	
	expr "$PID" = "" >/dev/null
	if [ "$?" -eq "0" ]
	then
		echo "Fetion:$f_account is not exist!"
		echo `date` >> /tmp/fxcron.log
		echo "Fetion:$f_account is not exist!" >> /tmp/fxcron.log

		LD_LIBRARY_PATH=. ./fetion --mobile=$f_account --pwd=$f_pwd --robotmode --command-path=$CMD_PATH >/dev/null 2>&1 &
	else
		CPU=`top -p $PID -d 1 -n 1|grep fetion|awk '{print $10}'`
		echo "Fetion:$f_account, cpu=$CPU"
		DIFF=`expr $CPU \> 50` > /dev/null
		if [ "$DIFF" -eq "1" ]
		then
			kill -9 $PID > /dev/null
			LD_LIBRARY_PATH=. ./fetion --mobile=$f_account --pwd=$f_pwd --robotmode --command-path=$CMD_PATH >/dev/null 2>&1 &
			echo `date` >> /tmp/fxcron.log
			echo "Fetion:$f_account CPU or memory!" >> /tmp/fxcron.log
		else
			# check tcp ip connect
			netstat -apn|grep :8080|grep ESTABLISHED|grep $PID/fetion
			if [ "$?" -eq "1" ]
			then
				echo `date` >> /tmp/fxcron.log
				echo "Fetion:$f_account tcp disconnect!" >> /tmp/fxcron.log
				kill -9 $PID > /dev/null
				LD_LIBRARY_PATH=. ./fetion --mobile=$f_account --pwd=$f_pwd --robotmode --command-path=$CMD_PATH >/dev/null 2>&1 &
			fi
		fi
	fi
done
